#include <cstdlib>

#include "coin2/exchange/base/market/enums.h"
#include "coin2/exchange/base/symbology/product_holder_manager.h"
#include "coin2/exchange/base/symbology/util.h"
#include "coin2/exchange/factory/symbology/product.h"
using ExchangeType = coin2::exchange::base::market::ExchangeType;
using MarketType = coin2::exchange::base::market::MarketType;
using coin::proto::ProductHolderManagerConfig;
using coin2::exchange::base::symbology::ProductHolderManager;
using coin2::exchange::base::symbology::ProductInfoConstantsManager;
using MarketExchangeApi = coin2::exchange::base::market::MarketExchangeApi;

int main() {
  ProductHolderManagerConfig config;
  config.set_root_dir("~/workspace/coin/experimental/qiang/product/pi");
  ProductHolderManager product_manager(config);

  product_manager.SetHotUpdateStatus(true);

  int64_t timestamp = 1691366400000000000;  // 2023-08-07T00:00:00Z
  auto mea = MarketExchangeApi::FromString("Futures.Prex.v1");
  auto product = CreateProductFromNormString(mea, std::string("YGG-USDT.PERPETUAL"), timestamp);

  std::string restore_cmd =
      "git restore  experimental/qiang/product/pi/20230807/Futures.Prex.v1.json";

  std::system(restore_cmd.c_str());

  int i = 0;
  while (1) {
    try {
      auto product_holder = product_manager.GetProductHolder(*product, timestamp);
      std::cout << product->native_symbol() << std::endl;
      std::cout << product_holder->product().relative_norm() << std::endl;
      std::cout << product_holder->product_info().contract_value() << std::endl;
    } catch (const std::runtime_error& e) {
      std::cout << "std::runtime_error: " << e.what() << std::endl;
      sleep(2);

      if (i == 2) {
        std::cout << "update pi file " << std::endl;
        std::string update_pi_cmd = "python3 experimental/qiang/product/new_pi_writer.py";
        std::system(update_pi_cmd.c_str());  // update pi file (add new symbol YGG-USDT.PERPETUAL)
      }
      i += 1;
      continue;
    }

    break;
  }
  return 0;
}
